#!/bin/bash

# FunASR CPU vs GPU 性能测试启动脚本

cd "$(dirname "$0")/.."

set -e

echo "🎯 FunASR 性能测试启动脚本"
echo "================================="

# 检查依赖
echo "🔍 检查环境依赖..."

# 检查Python环境
if ! command -v uv run python &> /dev/null; then
    echo "❌ uv run python 未安装"
    exit 1
fi

# 检查测试音频文件
if [ ! -f "assets/asr_vad_punc_example.wav" ]; then
    echo "❌ 测试音频文件不存在: assets/asr_vad_punc_example.wav"
    exit 1
fi

# 检查配置文件
if [ ! -f "src/speech_convert/config/funasr_config.py" ]; then
    echo "❌ 配置文件不存在: src/speech_convert/config/funasr_config.py"
    exit 1
fi

# 检查HTML测试文件
if [ ! -f "test_performance.html" ]; then
    echo "❌ HTML测试文件不存在: test_performance.html"
    exit 1
fi

echo "✅ 环境检查通过"

# 显示当前设备配置
echo ""
echo "📊 当前设备配置:"
uv run python scripts/switch_device_config.py --mode status

# 检查设备支持
echo ""
echo "🔍 设备支持检查:"
uv run python scripts/switch_device_config.py --mode check

echo ""
echo "🚀 准备启动测试..."
echo ""

# 选择测试模式
echo "请选择测试模式:"
echo "1. CPU 模式测试"
echo "2. GPU 模式测试"
echo "3. 完整对比测试 (CPU + GPU)"
echo "4. 仅启动WebSocket服务器"
echo "5. 仅打开HTML测试页面"
echo ""

read -p "请输入选择 (1-5): " choice

case $choice in
    1)
        echo "🔄 切换到CPU模式..."
        uv run python scripts/switch_device_config.py --mode cpu
        echo ""
        echo "🚀 启动CPU模式WebSocket服务器..."
        echo "服务器地址: ws://localhost:10095"
        echo "请在另一个终端中打开 test_performance.html 进行测试"
        echo ""
        cd speech_server
        uv run python funasr_wss_server.py --certfile "" --keyfile ""
        ;;
    2)
        echo "🔄 切换到GPU模式..."
        uv run python scripts/switch_device_config.py --mode gpu
        echo ""
        echo "🚀 启动GPU模式WebSocket服务器..."
        echo "服务器地址: ws://localhost:10095"
        echo "请在另一个终端中打开 test_performance.html 进行测试"
        echo ""
        cd speech_server
        uv run python funasr_wss_server.py --certfile "" --keyfile ""
        ;;
    3)
        echo "🔄 完整对比测试模式"
        echo ""
        echo "第一步: CPU模式测试"
        echo "====================="
        uv run python scripts/switch_device_config.py --mode cpu
        echo ""
        echo "📝 请按以下步骤进行测试："
        echo "1. 在新终端中运行: cd speech_server && uv run python funasr_wss_server.py"
        echo "2. 在浏览器中打开 test_performance.html"
        echo "3. 上传音频文件 assets/asr_vad_punc_example.wav"
        echo "4. 连接服务器并运行测试"
        echo "5. 记录CPU模式测试结果"
        echo "6. 停止服务器"
        echo ""
        read -p "CPU模式测试完成后按回车继续..."
        echo ""
        echo "第二步: GPU模式测试"
        echo "====================="
        uv run python scripts/switch_device_config.py --mode gpu
        echo ""
        echo "📝 请重复上述步骤进行GPU模式测试"
        echo "1. 重启服务器: cd speech_server && uv run python funasr_wss_server.py"
        echo "2. 刷新浏览器页面"
        echo "3. 重新运行测试"
        echo "4. 对比两次测试结果"
        echo ""
        echo "✅ 完整测试流程完成！"
        ;;
    4)
        echo "🚀 启动WebSocket服务器..."
        echo "服务器地址: ws://localhost:10095"
        echo "配置文件: config/funasr_config.py"
        echo ""
        cd speech_server
        uv run python funasr_wss_server.py --certfile "" --keyfile ""
        ;;
    5)
        echo "🌐 打开HTML测试页面..."
        # 尝试在默认浏览器中打开HTML文件
        if command -v open &> /dev/null; then
            # macOS
            open "test_performance.html"
        elif command -v xdg-open &> /dev/null; then
            # Linux
            xdg-open "test_performance.html"
        else
            echo "请手动在浏览器中打开文件: $(pwd)/test_performance.html"
        fi
        ;;
    *)
        echo "❌ 无效选择，退出"
        exit 1
        ;;
esac

echo ""
echo "🎉 测试完成！"

# 清理工作
if [ -f "config/funasr_config.py.backup" ]; then
    echo ""
    read -p "是否恢复原始配置文件？(y/n): " restore
    if [ "$restore" = "y" ] || [ "$restore" = "Y" ]; then
        uv run python switch_device_config.py --mode restore
        echo "✅ 配置文件已恢复"
    fi
fi

echo ""
echo "📊 测试结果保存在 performance_test_results/ 目录中"
echo "�� 详细信息请查看 性能测试文档.md"
